<?php

namespace App\Admin\Controllers\Admin;

use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use App\Models\Setting as SettingModel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

class Setting extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '网站设置';

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {

        $audit_switch = $request->audit_switch == 'on' ? 1 : 0;
        SettingModel::query()->updateOrCreate(['key' => 'audit_switch'], ['value' => json_encode($audit_switch)]);
        SettingModel::query()->updateOrCreate(['key' => 'bottom_price'], ['value' => json_encode($request->bottom_price)]);
        SettingModel::query()->updateOrCreate(['key' => 'speed_order_rate'], ['value' => json_encode($request->speed_order_rate)]);
        SettingModel::query()->updateOrCreate(['key' => 'bidding_flow_rate'], ['value' => json_encode($request->bidding_flow_rate)]);
        SettingModel::query()->updateOrCreate(['key' => 'order_off_rate'], ['value' => json_encode($request->order_off_rate)]);
        SettingModel::query()->updateOrCreate(['key' => 'pre_use'], ['value' => json_encode($request->pre_use)]);
        SettingModel::query()->updateOrCreate(['key' => 'jump_num'], ['value' => json_encode($request->jump_num)]);
        SettingModel::query()->updateOrCreate(['key' => 'divide_rate'], ['value' => json_encode($request->divide_rate)]);
        SettingModel::query()->updateOrCreate(['key' => 'api_url'], ['value' => json_encode($request->api_url)]);
        $domain_change_switch = $request->domain_change_switch == 'on' ? 1 : 0;
        SettingModel::query()->updateOrCreate(['key' => 'domain_change_switch'], ['value' => json_encode($domain_change_switch)]);
        return back();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->divider('订单设置');
        $this->switch('audit_switch', '订单审核开关')->states([
            'on' => ['value' => 1, 'text' => '开', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '关', 'color' => 'default'],
        ]);
        $this->number('bottom_price', '订单底价')->style('width','150px');
        $this->rate('speed_order_rate', '快单上浮比例');
        $this->rate('bidding_flow_rate', '竞价流量比例');
        $this->rate('order_off_rate', '订单自动关闭比例')->help('系统检测到订单完成度到达该比例后，将会自动关闭');
        $this->rate('divide_rate', '分成比例')->help('结算给流量主的分成比例');
        $this->text('api_url', '接口地址')->help('流量主接口地址');

        $this->divider('域名设置');
        $this->number('pre_use', '随机使用前几个')->style('width','150px');
        $this->switch('domain_change_switch', '域名轮转开关')->states([
            'on' => ['value' => 1, 'text' => '开', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '关', 'color' => 'default'],
        ]);

        $this->divider('跳转设置');
        $this->number('jump_num', '跳转次数')->style('width','150px');


    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {

        $audit_switch = SettingModel::query()->firstOrCreate(['key' => 'audit_switch'], ['value' => null]);
        $bottom_price = SettingModel::query()->firstOrCreate(['key' => 'bottom_price'], ['value' => null]);
        $speed_order_rate = SettingModel::query()->firstOrCreate(['key' => 'speed_order_rate'], ['value' => null]);
        $bidding_flow_rate = SettingModel::query()->firstOrCreate(['key' => 'bidding_flow_rate'], ['value' => null]);
        $order_off_rate = SettingModel::query()->firstOrCreate(['key' => 'order_off_rate'], ['value' => null]);
        $pre_use = SettingModel::query()->firstOrCreate(['key' => 'pre_use'], ['value' => null]);
        $jump_num = SettingModel::query()->firstOrCreate(['key' => 'jump_num'], ['value' => null]);
        $divide_rate = SettingModel::query()->firstOrCreate(['key' => 'divide_rate'], ['value' => null]);
        $api_url = SettingModel::query()->firstOrCreate(['key' => 'api_url'], ['value' => null]);
        $domain_change_switch = SettingModel::query()->firstOrCreate(['key' => 'domain_change_switch'], ['value' => null]);

        return [
            'audit_switch' => json_decode($audit_switch->value),
            'bottom_price'=> json_decode($bottom_price->value),
            'speed_order_rate'=> json_decode($speed_order_rate->value),
            'bidding_flow_rate'=> json_decode($bidding_flow_rate->value),
            'order_off_rate'=> json_decode($order_off_rate->value),
            'pre_use' => json_decode($pre_use->value),
            'jump_num' => json_decode($jump_num->value),
            'divide_rate' => json_decode($divide_rate->value),
            'api_url' => json_decode($api_url->value),
            'domain_change_switch' => json_decode($domain_change_switch->value),
        ];
    }
}
